package com.aizhixin.lab.post.controller;

import com.aizhixin.lab.account.dto.AccountDTO;
import com.aizhixin.lab.account.service.AccountService;
import com.aizhixin.lab.common.core.ApiReturnConstants;
import com.aizhixin.lab.common.utils.TokenUtil;
import com.aizhixin.lab.post.domain.PostSkillDomain;
import com.aizhixin.lab.post.domain.SkillColorDomain;
import com.aizhixin.lab.post.server.PostSkillService;
import com.aizhixin.lab.project.template.domain.ProjectModuleDomain;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/post/skill")
@Api(value = "岗位技能管理", description = "岗位技能管理")
public class PostSkillController {
    @Autowired
    private PostSkillService skillService;
    @Autowired
    private AccountService accountService;

    @RequestMapping(value = "/findSkillList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "根据岗位Id获取该岗位下的技能列表", httpMethod = "GET", notes = "根据岗位Id获取该岗位下的技能列表<br><br><b>lwq</b>")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = PostSkillDomain.class)})
    public ResponseEntity<?> findModuleList(@RequestHeader("Authorization") String token, @RequestParam String postId) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try{
            List<PostSkillDomain> postSkillDomain = skillService.findPostSkillListDomain(postId);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA,postSkillDomain);
        }catch (Exception e){
            result.put(ApiReturnConstants.SUCCESS, false);
        }

        return new ResponseEntity(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/deleteSkill", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "删除技能", httpMethod = "DELETE", notes = "删除技能<br><br><b>lwq</b>")
    public ResponseEntity<?> deleteSkill(@RequestHeader("Authorization") String token, @RequestBody String id) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try{
            skillService.deleteSkill(id);
            result.put(ApiReturnConstants.SUCCESS, true);
        }catch (Exception e){
            result.put(ApiReturnConstants.SUCCESS, false);
        }

        return   new ResponseEntity(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "新增技能", httpMethod = "POST", notes = "新增技能<br><br><b>lwq</b>")
    public ResponseEntity<?> save(@RequestHeader("Authorization") String token, @RequestBody PostSkillDomain domain) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try{
            skillService.saveDomain(domain);
            result.put(ApiReturnConstants.SUCCESS, true);
        }catch (Exception e){
            result.put(ApiReturnConstants.SUCCESS, false);
        }

        return   new ResponseEntity(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "修改技能", httpMethod = "POST", notes = "修改技能<br><br><b>lwq</b>")
    public ResponseEntity<?> update(@RequestHeader("Authorization") String token, @RequestBody PostSkillDomain domain) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try{
            skillService.updateDomain(domain);
            result.put(ApiReturnConstants.SUCCESS, true);
        }catch (Exception e){
            result.put(ApiReturnConstants.SUCCESS, false);
        }

        return   new ResponseEntity(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/findSkill", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查看技能详情", httpMethod = "GET", notes = "查看技能详情<br><br><b>lwq</b>")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = PostSkillDomain.class)})
    public ResponseEntity<?> findSkill(@RequestHeader("Authorization") String token, @RequestParam String domain) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try{
            PostSkillDomain postSkillDomain = skillService.findPostSkillDomain(domain);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA,postSkillDomain);
        }catch (Exception e){
            result.put(ApiReturnConstants.SUCCESS, false);
        }

        return new ResponseEntity(result, HttpStatus.OK);
    }

}
